package com.um.swing.bean;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.awt.*;
import java.util.HashMap;
import java.io.Serializable;
import java.util.Map;

/**
 * 图片实体
 */
public class ImageItem extends BaseConfigPostionSetBean implements Serializable, Cloneable {
    public static final String P1 = "p1";
    public static final String P2 = "p2";
    public static final String P3 = "p3";
    public static final String P4 = "p4";
    public static final String P5 = "p5";
    public static final String P6 = "p6";
    public static final String P7 = "p7";
    public static final String P8 = "p8";
    //p1,p2,p3,p4 || p5,p6,p7,p8
    private Map<String, Point> pointMap;

    private String path;
    private String name;
    @JsonIgnore
    private Image previewImage;//拼接后的预览图
    private boolean isPreview;//是否预览状态
    @JsonIgnore
    private int oldPointIndex = 1; //之前点的索引
    private int pointIndex = 1; //当前点的索引，标致着当前在操作哪个点

    private boolean isTowPage = false;  //是否一页两面

    public ImageItem() {
    }

    @JsonCreator
    public ImageItem(@JsonProperty("p1") Point p1,
                     @JsonProperty("p2") Point p2,
                     @JsonProperty("p3") Point p3,
                     @JsonProperty("p4") Point p4) {
        if (p1 != null) {
            putPointMapItem(P1, p1);
        }
        if (p2 != null) {
            putPointMapItem(P2, p2);
        }
        if (p3 != null) {
            putPointMapItem(P3, p3);
        }
        if (p4 != null) {
            putPointMapItem(P4, p4);
        }
    }

    public ImageItem(String path, String name) {
        this.path = path;
        this.name = name;
    }

    public int getOldPointIndex() {
        return oldPointIndex;
    }

    public void setOldPointIndex(int oldPointIndex) {
        this.oldPointIndex = oldPointIndex;
    }

    public boolean isPreview() {
        return isPreview;
    }

    public void setPreview(boolean preview) {
        isPreview = preview;
    }

    public Image getPreviewImage() {
        return previewImage;
    }

    public void setPreviewImage(Image previewImage) {
        this.previewImage = previewImage;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPointIndex() {
        return pointIndex;
    }

    public void setPointIndex(int pointIndex) {
        this.pointIndex = pointIndex;
    }

    public boolean isTowPage() {
        return isTowPage;
    }

    public void setTowPage(boolean towPage) {
        isTowPage = towPage;
    }

    public Map<String, Point> getPointMap() {
        return pointMap;
    }

    public void setPointMap(Map<String, Point> pointMap) {
        this.pointMap = pointMap;
    }

    @JsonIgnore
    public void putPointMapItem(String key, Point point) {
        if (this.pointMap == null) {
            this.pointMap = new HashMap<>();
        }
        this.pointMap.put(key, point);
    }

    @JsonIgnore
    public Point getPointMapItem(String key) {
        if (this.pointMap == null) {
            return null;
        }
        return this.pointMap.get(key);
    }

    /**
     * 判断4个点位是否落实
     *
     * @return
     */
    @JsonIgnore
    public boolean isOverPoint() {
        return pointMap != null && !ObjectUtil.hasNull(pointMap.get(P1), pointMap.get(P2), pointMap.get(P3), pointMap.get(P4));
    }

    //获取边线的上点
    @JsonIgnore
    public Point sideTopPoint() {
        Point p1 = pointMap.get(P1);
        Point p2 = pointMap.get(P2);
        return p1.y > p2.y ? p2 : p1;
    }

    //获取顶线的上点
    @JsonIgnore
    public Point lineTopPoint() {
        Point p3 = pointMap.get(P3);
        Point p4 = pointMap.get(P4);
        return p3.y > p4.y ? p4 : p3;
    }
}
